package com.bkd.dreams.server.event;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;

import com.bkd.dreams.client.event.VolunteerEvent;
import com.bkd.dreams.client.event.VolunteerEventDTO;
import com.bkd.dreams.client.event.service.VolunteerEventService;
import com.bkd.dreams.client.login.NotLoggedInException;
import com.bkd.dreams.client.volunteer.Volunteer;
import com.bkd.dreams.client.volunteer.VolunteerDTO;
import com.bkd.dreams.server.PMF;
import com.bkd.dreams.server.core.RemoteServiceImpl;
import com.bkd.dreams.server.volunteer.VolunteerHelper;
import com.google.appengine.api.users.User;

public class VolunteerEventServiceImpl extends RemoteServiceImpl implements VolunteerEventService {

	@Override
	public List<VolunteerEventDTO> getAllEvents() throws NotLoggedInException {
		checkLoggedIn();
		List<VolunteerEventDTO> eventList = new ArrayList<VolunteerEventDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			List<VolunteerEvent> queryResults = (List<VolunteerEvent>)pm.newQuery("select from " + VolunteerEvent.class.getName()).execute();
			for(VolunteerEvent event: queryResults) {
				eventList.add(new VolunteerEventDTO(event));
			}
		} finally {
			pm.close();
		}
		return eventList;
	}

	@Override
	public void addEvent(VolunteerEventDTO eventDto) throws NotLoggedInException {
		checkLoggedIn();
		VolunteerEvent event = new VolunteerEvent(eventDto);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(event);
		} finally {
			pm.close();
		}
	}
	
	@Override
	public void removeEvent(VolunteerEventDTO event) throws NotLoggedInException {
		checkLoggedIn();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			VolunteerEvent obj = pm.getObjectById(VolunteerEvent.class, event.getKey());
			pm.deletePersistent(obj);
		} finally {
			pm.close();
		}
	}

	@Override
	public void signUp(VolunteerEventDTO eventDto, VolunteerDTO volDto) throws NotLoggedInException {
		checkLoggedIn();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		VolunteerEvent event = pm.getObjectById(VolunteerEvent.class, eventDto.getKey());
		try {
			Volunteer volunteer = VolunteerHelper.getPersistableVolunteerByUserId(volDto.getUserId());
			volunteer.addEvent(event);
			event.addVolunteer(volunteer);
			pm.makePersistent(event);
			pm.makePersistent(volunteer);
		} finally {
			pm.close();
		}
	}
	
	@Override
	public void removeSignUp(VolunteerEventDTO eventDto, VolunteerDTO volDto) throws NotLoggedInException {
		checkLoggedIn();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		VolunteerEvent event = pm.getObjectById(VolunteerEvent.class, eventDto.getKey());
		try {
			Volunteer volunteer = VolunteerHelper.getPersistableVolunteerByUserId(volDto.getUserId());
			volunteer.removeEvent(event);
			event.removeVolunteer(volunteer);
			pm.makePersistent(event);
			pm.makePersistent(volunteer);
		} finally {
			pm.close();
		}
	}

}
